home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / AMarquee / examples / BounceCount.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  3KB  |  105 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <clib/dos_protos.h>
  8. #include <clib/exec_protos.h>
  9.  
  10. #include <clib/AMarquee_protos.h>
  11.  
  12. #include <pragmas/AMarquee_pragmas.h>
  13.  
  14. struct Library * AMarqueeBase = NULL;
  15. struct QSession * session     = NULL;
  16.  
  17. void CleanExit(void)
  18. {
  19.   if (session)      QFreeSession(session);       /* This MUST be done before we close the library! */
  20.   if (AMarqueeBase) CloseLibrary(AMarqueeBase);
  21.   printf("All done.\n");
  22. }
  23.  
  24. /* Main program--simply increments our counter whenever someone else increments theirs */
  25. int main(int argc, char ** argv)
  26. {
  27.   char * connectTo, * progName;
  28.   int port = 2957;
  29.   int count = 0;
  30.   char temp[700];
  31.   BOOL BDie = FALSE;
  32.   
  33.   atexit(CleanExit);
  34.  
  35.   printf("Usage Note:  BounceCount [hostname=localhost] [myname=bounce]\n");
  36.   
  37.   connectTo = (argc > 1) ? argv[1] : "localhost";
  38.   progName  = (argc > 2) ? argv[2] : "bounce";
  39.  
  40.   if ((AMarqueeBase = OpenLibrary("amarquee.library",37L)) == NULL)
  41.   {
  42.     printf("Couldn't open amarquee.library v37!\n");
  43.     exit(RETURN_ERROR);
  44.   }
  45.   printf("Connecting to %s:%i...\n",connectTo, port);
  46.   if ((session = QNewSession(connectTo, port, progName)) == NULL)
  47.   {
  48.     printf("Couldn't connect to server %s:%i\n",connectTo, port);
  49.     CloseLibrary(AMarqueeBase);
  50.     exit(RETURN_WARN);
  51.   }
  52.   
  53.   printf("BounceCount connected to server %s:%i\n",connectTo, port);
  54.  
  55.   /* Setup */
  56.   sprintf(temp,"/#?/~(%s)/count",progName);
  57.   (void)QSetOp(session, "count", "0", 2);
  58.   (void)QSubscribeOp(session, temp, -1);
  59.   (void)QGetOp(session, temp, -1L);
  60.   (void)QGo(session,0L);
  61.  
  62.   while(BDie == FALSE)
  63.   {
  64.     struct QMessage * qMsg;
  65.     ULONG signals = (1L << session->qMsgPort->mp_SigBit) | (SIGBREAKF_CTRL_C);
  66.  
  67.     /* Wait for next message from the server */
  68.     signals = Wait(signals);
  69.     
  70.     if (signals & (1L << session->qMsgPort->mp_SigBit))
  71.     {
  72.       while(qMsg = (struct QMessage *) GetMsg(session->qMsgPort))
  73.       {
  74.         if (qMsg->qm_Status != QERROR_NO_ERROR) 
  75.         {
  76.           printf("Error %i detected!\n", qMsg->qm_Status);
  77.           BDie = TRUE;
  78.         }
  79.         else
  80.         {
  81.           if ((qMsg->qm_Path)&&(qMsg->qm_Data))
  82.           {
  83.             /* Must be in response to our subscription! */
  84.             int val = atoi(qMsg->qm_Data);
  85.             
  86.             /* Raise the stakes a bit :) */
  87.             printf("I see a %i... ",val); 
  88.             if (val >= count)
  89.             {
  90.               printf("and I raise it to %i!\n",val+1); 
  91.               sprintf(temp,"%i",val+1);
  92.               (void) QSetOp(session, "count", temp, strlen(temp)+1);
  93.               (void) QGo(session,0L);
  94.               count = val+1;
  95.             }
  96.             else printf("but my count of %i is higher.\n",count); 
  97.           }
  98.         }
  99.         FreeQMessage(session,qMsg);
  100.       }
  101.     }
  102.     if (signals & SIGBREAKF_CTRL_C) BDie = TRUE;  /* Quit if CTRL-C pressed */
  103.   }
  104. }
  105.